﻿using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using NetCore.BLL;
using NetCore.Model;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;

namespace NetCore.Api.Web.Controllers
{

    /// <summary>
    /// 
    /// </summary>
    
    [MenuPage(200, "A002", "用户管理", "Admin/User", "fa-user")]
    [ApiVersion("1")]
    [Route("api/admin/user")]
    public class UserController : ControllerBase
    { 
        #region Service注入

        public IUserService mService { get; set; }
        #endregion

        #region 模版生成
        /// <summary>
        /// Grid 分页查询方法
        /// </summary>
        /// <param name="grid">分页参数</param>
        /// <param name="data">其他参数</param>
        /// <returns></returns>
        [HttpPost]
        [Permission(IsParent =true)]

        public ActionResult<string> ForGrid(LigerGrid grid)
        {
            return JSONHelper.ToJson(AjaxResult.Success(mService.ForGrid(grid)));
        }

        public ActionResult<string> Get(string id)
        {
           return JSONHelper.ToJson(mService.GetById(id));
        }

        /// <summary>
        /// 保存 编辑方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Permission("01", "编辑", IsGet = false)]
        public ActionResult<string> Put([FromBody] UserEntity entity)
        {

            string strError = "";
            if (ModelState.IsValid)
            {
                if (mService.Save(entity, ref strError))
                {
                    return JSONHelper.ToJson(AjaxResult.Success());
                }
                ModelState.AddModelError(string.Empty, strError);
            }
            return JSONHelper.ToJson(AjaxResult.Error(ModelState));

        }
     
        /// <summary>
        /// 批量删除方法
        /// </summary>
        /// <param name="deleteKeys">主键列表</param>
        /// <returns></returns>
        [Permission("02", "删除")]
        public ActionResult<string> Delete(string id)
        {
            string strError = "";
            if (mService.Delete(id, ref strError))
            {
                return JSONHelper.ToJson(AjaxResult.Success());
            }
            return JSONHelper.ToJson(AjaxResult.Error(ModelState));
        }
        #endregion
        #region 
       
        [Permission("04", "导出", IsParent = true)]
        public IActionResult Export()
        {
            string strError = "";
            byte[] buffer=mService.Export(ref strError);
            return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",DateTime.Now.ToString("yyyyMMddHHmmss")+".xlsx");
        
        }
        #endregion
    }
}
